package com.zebra.sdl;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.TwoStatePreference;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.jb.SymbologiesPrefer;

import com.example.ht380d4G_jar_demo.R;
import com.zebra.adc.decoder.BarCodeReader;
import com.zebra.sdl.SdlScanService.MyBinder;

@SuppressWarnings("deprecation")
public class SymbologiesActivity extends PreferenceActivity implements
        Preference.OnPreferenceClickListener,Preference.OnPreferenceChangeListener{

    private static final String TAG = "SymbologiesActivity";
	private Preference mEnableAll;
    private Preference mDisableAll;
    private PreferenceScreen mSymbologies;
    private CheckBoxPreference mAztec,mBc412,mCodabar,mCodablockf,mCode11,mCode128,mCode32,
    mCode39,mCode49,mCode93,mData_matrix,mEan13,mEan8,mGrid_matrix,mGs1_databar,
    mHanxin_code,mHong_kong_2_of_5,mIata_2_of_5,mInterleaved_2_of_5,mMatrix_2_of_5,
    mMaxi_code,mMicropdf417,mMicroqr,mMsi_plessey,mNec_2_of_5,mPdf417,mPharmacode;
    private BarCodeReader bcr = null;
	private SdlScanService scanService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the XML preferences file
        addPreferencesFromResource(R.xml.symbologies);
    }
    
    protected void onResume(){    	
 		Intent service = new Intent(this, SdlScanService.class);
 		bindService(service, serviceConnection, BIND_AUTO_CREATE);
 		super.onResume();
    }
    private void initView(){
    	mSymbologies = (PreferenceScreen) getPreferenceScreen().findPreference(
                "symbologies");
        mEnableAll = (Preference) getPreferenceScreen().findPreference(
                "enable_all");
        mEnableAll.setOnPreferenceClickListener(this);
        mDisableAll = (Preference) getPreferenceScreen().findPreference(
                "disable_all");
        mDisableAll.setOnPreferenceClickListener(this);
        mAztec = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "aztec");
        mAztec.setOnPreferenceChangeListener(this);
        mBc412 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "bc412");
        mBc412.setOnPreferenceChangeListener(this);
        mCodabar = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "codabar");
        mCodabar.setOnPreferenceChangeListener(this);
        mCodablockf = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "codablockf");
        mCodablockf.setOnPreferenceChangeListener(this);
        mCode11 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "code11");
        mCode11.setOnPreferenceChangeListener(this);
        mCode128 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "code128");
        mCode128.setOnPreferenceChangeListener(this);
        mCode32 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "code32");
        mCode32.setOnPreferenceChangeListener(this);
        mCode39 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "code39");
        mCode39.setOnPreferenceChangeListener(this);
        mCode49 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "code49");
        mCode49.setOnPreferenceChangeListener(this);
        mCode93 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "code93");
        if(SymbologiesPrefer.getCode93(this, 1)==1){
        	mCode93.setChecked(true);
        }else {
        	mCode93.setChecked(false);
        }
        mCode93.setOnPreferenceChangeListener(this);
        mData_matrix = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "data_matrix");
        mData_matrix.setOnPreferenceChangeListener(this);
        mEan13 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "ean13");
        mEan13.setOnPreferenceChangeListener(this);
        mEan8 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "ean8");
        mEan8.setOnPreferenceChangeListener(this);
        mGrid_matrix = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "grid_matrix");
        mGrid_matrix.setOnPreferenceChangeListener(this);
       
        mGs1_databar = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "gs1_databar");
        mGs1_databar.setOnPreferenceChangeListener(this);
        
        mHanxin_code = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "hanxin_code");
        mHanxin_code.setOnPreferenceChangeListener(this);

        mHong_kong_2_of_5 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "hong_kong_2_of_5");
        mHong_kong_2_of_5.setOnPreferenceChangeListener(this);

        mIata_2_of_5 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "iata_2_of_5");
        mIata_2_of_5.setOnPreferenceChangeListener(this);

       
        mInterleaved_2_of_5 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "interleaved_2_of_5");
        mInterleaved_2_of_5.setOnPreferenceChangeListener(this);
        
        mMatrix_2_of_5 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "matrix_2_of_5");
        mMatrix_2_of_5.setOnPreferenceChangeListener(this);
        
        mMaxi_code = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "maxi_code");
        mMaxi_code.setOnPreferenceChangeListener(this);
        
        mMicropdf417 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "micropdf417");
        mMicropdf417.setOnPreferenceChangeListener(this);
        
        mMicroqr = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "microqr");
        mMicroqr.setOnPreferenceChangeListener(this);
        
        mMsi_plessey = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "msi_plessey");
        mMsi_plessey.setOnPreferenceChangeListener(this);
        
        mNec_2_of_5 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "nec_2_of_5");
        mNec_2_of_5.setOnPreferenceChangeListener(this);
        
        mPdf417 = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "pdf417");
        mPdf417.setOnPreferenceChangeListener(this);
        
        mPharmacode = (CheckBoxPreference) getPreferenceScreen().findPreference(
                "pharmacode");
        mPharmacode.setOnPreferenceChangeListener(this);
    }
    
    ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.v(TAG,"onServiceConnected");
			MyBinder myBinder = (MyBinder) service;
			scanService = myBinder.getService();
			bcr = scanService.getBcr();
	    	initView();
		}
	};
	
    @Override
    public boolean onPreferenceClick(Preference preference) {
    	
        if (preference == mEnableAll) {
            // Enable all symbologies. CortexScan will detect the preference
            // change and do the actual work.
            int count = mSymbologies.getPreferenceCount();
            for (int i = 0; i < count; i++) {
                Preference p = mSymbologies.getPreference(i);
                if (p instanceof TwoStatePreference)
                    ((TwoStatePreference) p).setChecked(true);
            }
            return true;
        } else if (preference == mDisableAll) {
            // Disable all symbologies. CortexScan will detect the preference
            // change and do the actual work.
            int count = mSymbologies.getPreferenceCount();
            for (int i = 0; i < count; i++) {
                Preference p = mSymbologies.getPreference(i);
                if (p instanceof TwoStatePreference)
                    ((TwoStatePreference) p).setChecked(false);
            }
            return true;
        }
        return false;
        
    }

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		Log.v(TAG,"onPreferenceChange bcr: "+bcr);
		if (bcr!=null) {
        if(preference ==mAztec){
    	  if ((Boolean)newValue) {
    		  bcr.setParameter(7, 1);
         } else {
        	 bcr.setParameter(7, 0);
         }
    	  mAztec.setChecked((Boolean)newValue);
        } else if(preference ==mCode93){
    	  if ((Boolean)newValue) {
    		  bcr.setParameter(9, 1);
    		  SymbologiesPrefer.setCode93(this, 1);
         } else {
        	 bcr.setParameter(9, 0);
        	 SymbologiesPrefer.setCode93(this, 0);
         }
    	  mCode93.setChecked((Boolean)newValue);
      } else if(preference == mInterleaved_2_of_5){//交叉25
    	  if ((Boolean)newValue) {
    		  bcr.setParameter(6, 1);
         } else {
        	 bcr.setParameter(6, 0);
         }
    	  mInterleaved_2_of_5.setChecked((Boolean)newValue);
      }else if(preference ==mCode39){
    	  if ((Boolean)newValue) {
    		  bcr.setParameter(0, 1);
         } else {
        	 bcr.setParameter(0, 0);
         }
    	  mCode39.setChecked((Boolean)newValue);
      }else if(preference == mCode32){
    	  if ((Boolean)newValue) {
    		  bcr.setParameter(0, 1);
    		  bcr.setParameter(86, 1);
         } else {
        	 bcr.setParameter(0, 0);
         }
    	  mCode32.setChecked((Boolean)newValue);
      }else if(preference ==mGs1_databar){
    	  if ((Boolean)newValue) {
    		  bcr.setParameter(338, 1);
         } else {
        	 bcr.setParameter(338, 0);
         }
    	  mGs1_databar.setChecked((Boolean)newValue);
      }else if(preference ==mMatrix_2_of_5){//矩阵25
    	  if ((Boolean)newValue) {
    		  bcr.setParameter(618, 1);
         } else {
        	 bcr.setParameter(618, 0);
         }
    	  mMatrix_2_of_5.setChecked((Boolean)newValue);
      }else if(preference ==mCodabar){
    	  if ((Boolean)newValue) {
    		  bcr.setParameter(7, 1);
         } else {
        	 bcr.setParameter(618, 0);
         }
    	  mCodabar.setChecked((Boolean)newValue);
      }	
	}
		return false;
	}
	
	public void onDestory(){
		super.onDestroy();	
	}
	
	public void onPause(){
		unbindService(serviceConnection);
		super.onPause();
	}
}
